<?php


namespace app\api\controller;

use library\Controller;
use think\Db;
use think\Request;
use app\common\model\Car as CarModel;
use app\validate\CarCheck;
class Car extends Base
{
    //车辆添加
    public function add(Request $request,CarModel $car,CarCheck $check){
        try {
            $param = $request->param();
            $result = $check->scene('car_add')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $car->add($param);
                if ($list==1) {
                    return $this->api_result(200, '请求成功', $param);
                } else {
                    return $this->api_result(400, $list, $param);
                }
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }

    //车辆列表
    public function index(Request $request,CarModel $car,CarCheck $check){
        try {
            $param = $request->param();
            $result = $check->scene('car_index')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $car->index($param);
                if ($list) {
                    return $this->api_result(200, '请求成功', $list);
                } else {
                    return $this->api_result(400, $list, $param);
                }
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }

    //车辆修改
    public function update(Request $request,CarModel $car,CarCheck $check){
        try {
            $param = $request->param();
            $result = $check->scene('car_save')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $car->car_save($param);
                if ($list==1) {
                    return $this->api_result(200, '请求成功', $param);
                } else {
                    return $this->api_result(400, $list, $param);
                }
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }

    //车辆删除
    public function del(Request $request,CarModel $car,CarCheck $check){
        try {
            $param = $request->param();
            $result = $check->scene('car_del')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $car->del($param);
                if ($list==1) {
                    return $this->api_result(200, '请求成功', $param);
                } else {
                    return $this->api_result(400, $list, $param);
                }
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }

}